package cn.bisoft.java.service.ext.httpclient.common;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.bisoft.java.bean.message.MessageBody;
import cn.bisoft.java.bean.message.MessageHeader;
import cn.bisoft.java.bean.message.RequestMessageBody;
import cn.bisoft.java.bean.message.ResponseMessageBody;
import cn.bisoft.java.bean.message.TransMessage;
import cn.bisoft.java.constants.Constants;

import com.hengpeng.common.exception.ExceptionFactory;
import com.hengpeng.common.exception.MessageFormatException;
import com.hengpeng.common.exception.TransException;
import com.hengpeng.common.util.MD5;
import com.hengpeng.common.util.SignatureHelper;
import com.hengpeng.common.util.StringHelper;

public class MessageHelper implements Serializable
{
	private final static Log logger = LogFactory.getLog(MessageHelper.class);

	private static final long serialVersionUID = 5064762847227646676L;

	public static TransMessage createResponseMessage(TransMessage intm, int transType)
	{
		TransMessage tm = new TransMessage();
		tm.setId(intm.getId());
		tm.setVersion(intm.getVersion());

		MessageHeader header = new MessageHeader();
		header.setMessengerID(intm.getHeader().getMessengerID());
		header.setTransactionType(transType);

		try
		{
			String timeStr = StringHelper.getStringFromDate(new Date(), Constants.PROTOCOL_TIMESTAMP_FORMAT);
			header.setTimestamp(timeStr);
		}
		catch (ParseException e)
		{
		}
		tm.setHeader(header);

		MessageBody body = new ResponseMessageBody();
		tm.setBody(body);
		return tm;
	}

	public static TransMessage createResponseMessage(TransMessage intm, int transType, String rspCode)
	{
		TransMessage tm = new TransMessage();
		tm.setId(intm.getId());
		tm.setVersion(intm.getVersion());

		MessageHeader header = new MessageHeader();
		header.setMessengerID(intm.getHeader().getMessengerID());
		header.setTransactionType(transType);

		try
		{
			String timeStr = StringHelper.getStringFromDate(new Date(), Constants.PROTOCOL_TIMESTAMP_FORMAT);
			header.setTimestamp(timeStr);
		}
		catch (ParseException e)
		{
		}
		tm.setHeader(header);

		try
		{
			tm.setBody(MessageHelper.getResponseBodyByCode(rspCode));
		}
		catch (Exception e)
		{
			ResponseMessageBody body = new ResponseMessageBody();
			body.setCode(Constants.TRANSACTION_RESPONSE_CODE_9999);
			body.setMessage(Constants.TRANSACTION_RESPONSE_MESSAGE_9999);
			tm.setBody(body);
		}
		return tm;
	}

	public static TransMessage createResponseMessage(String xml, int transType, String rspCode)
	{
		TransMessage tm = new TransMessage();

		try
		{
			String id = MessageHelper.getMessageID(xml);
			if (id == null)
			{
				tm.setId("-1");
			}
			else
			{
				tm.setId(id);
			}

		}
		catch (Exception e)
		{
			tm.setId("-1");
		}

		try
		{
			String version = MessageHelper.getVersion(xml);
			if (version == null)
			{
				tm.setVersion("1.0");
			}
			else
			{
				tm.setVersion(version);
			}
		}
		catch (Exception e)
		{
			tm.setVersion("1.0");
		}

		MessageHeader header = new MessageHeader();
		try
		{
			header.setMessengerID(MessageHelper.getMessengerId(xml));
		}
		catch (Exception e)
		{
			header.setMessengerID("-1");
		}

		header.setTransactionType(transType);

		try
		{
			String timeStr = StringHelper.getStringFromDate(new Date(), Constants.PROTOCOL_TIMESTAMP_FORMAT);
			header.setTimestamp(timeStr);
		}
		catch (ParseException e)
		{
		}
		tm.setHeader(header);

		try
		{
			tm.setBody(MessageHelper.getResponseBodyByCode(rspCode));
		}
		catch (Exception e)
		{
			ResponseMessageBody body = new ResponseMessageBody();
			body.setCode(Constants.TRANSACTION_RESPONSE_CODE_9999);
			body.setMessage(Constants.TRANSACTION_RESPONSE_MESSAGE_9999);
			tm.setBody(body);
		}

		return tm;
	}

	/**
	 * <pre>
	 * ���������Ϣ�������Ӧ��Ϣ��
	 * </pre>
	 * @param xml ������Ϣ��
	 * @param transType ��������
	 * @param rspCode ��Ӧ״̬��
	 * @return ��Ӧ��Ϣ��
	 */
	public static TransMessage createResponseMessage(String xml, int transType, String rspCode, String extMessage)
	{
		TransMessage tm = new TransMessage();

		try
		{
			String id = MessageHelper.getMessageID(xml);
			if (id == null)
			{
				tm.setId("-1");
			}
			else
			{
				tm.setId(id);
			}

		}
		catch (Exception e)
		{
			logger.error("����Ϣ����ȡ��ϢID�쳣,��Ϣ��{" + xml + "}");
			tm.setId("-1");
		}

		try
		{
			String version = MessageHelper.getVersion(xml);
			if (version == null)
			{
				tm.setVersion("1.0");
			}
			else
			{
				tm.setVersion(version);
			}
		}
		catch (Exception e)
		{
			logger.error("����Ϣ����ȡ�汾���쳣,��Ϣ��{" + xml + "}");
			tm.setVersion("1.0");
		}

		MessageHeader header = new MessageHeader();
		try
		{
			header.setMessengerID(MessageHelper.getMessengerId(xml));
		}
		catch (Exception e)
		{
			logger.error("����Ϣ����ȡMessengerId�쳣,��Ϣ��{" + xml + "}");
			header.setMessengerID("-1");
		}

		header.setTransactionType(transType);

		try
		{
			String timeStr = StringHelper.getStringFromDate(new Date(), Constants.PROTOCOL_TIMESTAMP_FORMAT);
			header.setTimestamp(timeStr);
		}
		catch (ParseException e)
		{
			logger.error("��ʽ����Ϣͷ��timestamp�ֶ�ʱ�쳣", e);
		}
		tm.setHeader(header);

		try
		{
			tm.setBody(MessageHelper.getResponseBodyByCodeAndExtInfo(rspCode, extMessage));
		}
		catch (Exception e)
		{
			logger.error("ͨ����Ӧ�������Ϣ���쳣��", e);
			ResponseMessageBody body = new ResponseMessageBody();
			body.setCode(Constants.TRANSACTION_RESPONSE_CODE_9999);
			body.setMessage(Constants.TRANSACTION_RESPONSE_MESSAGE_9999);
			tm.setBody(body);
		}

		return tm;
	}

	/**
	 * <pre>
	 * ����Ϣ�����л�����Ϣ����ժҪ
	 * </pre>
	 * @param source ���л�����Ϣ��
	 * @param msgerPwd ����������
	 * @return ժҪ�����Ϣ���ַ�
	 */
	public static String digestRSAMessage(String source, String msgerPwd) throws MessageFormatException
	{
		try
		{
			StringBuffer digestSrc = new StringBuffer();
			digestSrc.append(MessageHelper.getMessageID(source));
			digestSrc.append(StringHelper.getContentByTag(source, Constants.MESSAGE_TIMESTAMP_TAG_BEGIN,
					Constants.MESSAGE_TIMESTAMP_TAG_END, false));
			digestSrc.append(StringHelper.getContentByTag(source, Constants.MESSAGE_BODY_TAG_BEGIN,
					Constants.MESSAGE_BODY_TAG_END, true));
			String digest = SignatureHelper.generateRSASignature(msgerPwd, digestSrc.toString(), Constants.MESSAGE_AGENTPORTAL_ENCODING);
			return StringHelper.replace(source, Constants.MESSAGE_DIGEST_TAG_BEGIN,
					Constants.MESSAGE_DIGEST_TAG_END, digest);
		}
		catch (Exception e)
		{
			logger.error("����Ϣ����ժҪʱ����!", e);
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0010);
		}
	}

	/**
	 * <pre>
	 * ����Ϣ�����л�����Ϣ����ժҪ
	 * </pre>
	 * @param source ���л�����Ϣ��
	 * @param msgerPwd ����������
	 * @return ժҪ�����Ϣ���ַ�
	 */
	public static String digestMessage(String source, String msgerPwd) throws MessageFormatException
	{
		try
		{
			StringBuffer digestSrc = new StringBuffer();
			digestSrc.append(MessageHelper.getMessageID(source));
			digestSrc.append(StringHelper.getContentByTag(source, Constants.MESSAGE_TIMESTAMP_TAG_BEGIN,
					Constants.MESSAGE_TIMESTAMP_TAG_END, false));
			digestSrc.append(msgerPwd);
			digestSrc.append(StringHelper.getContentByTag(source, Constants.MESSAGE_BODY_TAG_BEGIN,
					Constants.MESSAGE_BODY_TAG_END, true));
			String digest = MD5.encodeString(digestSrc.toString(), Constants.MESSAGE_AGENTPORTAL_ENCODING);

			return StringHelper.replace(source, Constants.MESSAGE_DIGEST_TAG_BEGIN,
					Constants.MESSAGE_DIGEST_TAG_END, digest);
		}
		catch (Exception e)
		{
			logger.error("����Ϣ����ժҪʱ����!", e);
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0010);
		}
	}

	/**
	 * <pre>
	 * ����Ϣ�����л�����Ϣ����ժҪ
	 * </pre>
	 * @param message ��Ϣ�����
	 * @param source ���л�����Ϣ��
	 * @return ժҪ�����Ϣ���ַ�
	 */
	public static String digestMessage(TransMessage message, String source, String msgerPwd, String encoding)
			throws MessageFormatException
	{
		try
		{
			StringBuffer digestSrc = new StringBuffer();
			digestSrc.append(message.getId());
			digestSrc.append(message.getHeader().getTimestamp());
			digestSrc.append(msgerPwd);
			digestSrc.append(StringHelper.getContentByTag(source, Constants.MESSAGE_BODY_TAG_BEGIN,
					Constants.MESSAGE_BODY_TAG_END, true));
			String digest = MD5.encodeString(digestSrc.toString(), encoding);

			return StringHelper.replace(source, Constants.MESSAGE_DIGEST_TAG_BEGIN,
					Constants.MESSAGE_DIGEST_TAG_END, digest);
		}
		catch (Exception e)
		{
			logger.error("����Ϣ����ժҪʱ����!", e);
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0010);
		}
	}

	/**
	 * <pre>
	 * ����Ϣ�����л�����Ϣ����ժҪ
	 * </pre>
	 * @param message ��Ϣ�����
	 * @param source ���л�����Ϣ��
	 * @return ժҪ�����Ϣ���ַ�
	 */
	public static String digestRSAMessage(TransMessage message, String source, String msgerPwd, String encoding)
			throws MessageFormatException
	{
		try
		{
			StringBuffer digestSrc = new StringBuffer();
			digestSrc.append(message.getId());
			digestSrc.append(message.getHeader().getTimestamp());
			digestSrc.append(StringHelper.getContentByTag(source, Constants.MESSAGE_BODY_TAG_BEGIN,
					Constants.MESSAGE_BODY_TAG_END, true));
			String digest = SignatureHelper.generateRSASignature(msgerPwd, digestSrc.toString(), encoding);
			return StringHelper.replace(source, Constants.MESSAGE_DIGEST_TAG_BEGIN,
					Constants.MESSAGE_DIGEST_TAG_END, digest);
		}
		catch (Exception e)
		{
			logger.error("����Ϣ����ժҪʱ����!", e);
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0010);
		}
	}

	/**
	 * <pre>
	 * ��ݸ���Ϣ��xml�ַ��ȡbody��������
	 * </pre>
	 * @param source ��Ϣ��xml�ַ�
	 * @param includeTag �Ƿ����
	 * @return body��������
	 */
	public static String getContentBody(String source, boolean includeTag)
	{
		return StringHelper.getContentByTag(source, Constants.MESSAGE_BODY_TAG_BEGIN,
				Constants.MESSAGE_BODY_TAG_END, includeTag);
	}

	/**
	 * <pre>
	 * ��ݸ���Ϣ��xml�ַ��ȡժҪ��������
	 * </pre>
	 * @param source ��Ϣ��xml�ַ�
	 * @param includeTag �Ƿ����
	 * @return ժҪ��������
	 */
	public static String getDigest(String source, boolean includeTag)
	{
		return StringHelper.getContentByTag(source, Constants.MESSAGE_DIGEST_TAG_BEGIN,
				Constants.MESSAGE_DIGEST_TAG_END, includeTag);
	}

	/**
	 * <pre>
	 * ��ȡ��Ϣ���ַ�����Ϣ��ı��
	 * </pre>
	 * @param xmlString ��Ϣ���ַ�
	 * @return ��Ϣ��ı��
	 */
	public static String getMessageID(String xmlString)
	{
		String id = null;
		String flag = Constants.MESSAGE_MESSAGEID_TAG;
		int index = xmlString.indexOf(flag);
		if (-1 != index)
		{
			String tmp = xmlString.substring(index + flag.length());
			index = tmp.indexOf("\"");
			id = tmp.substring(0, index);
		}
		return id;
	}

	/**
	 * <pre>
	 * ����������Ϣ���ַ��ȡmessageIDֵ
	 * </pre>
	 * @param xml ������Ϣ���ַ�
	 * @return messageIDֵ
	 */
	public static String getMessengerId(String xml)
	{
		return StringHelper.getContentByTag(xml, Constants.MESSAGE_MESSENGERID_TAG_BEGIN,
				Constants.MESSAGE_MESSENGERID_TAG_END, false);
	}

	/**
	 * <pre>
	 * �����Ӧ�������Ӧ��Ϣ��
	 * </pre>
	 * @param code ��Ӧ��
	 * @return ��Ӧ��Ϣ��
	 */
	public static ResponseMessageBody getResponseBodyByCode(String code)
	{
		ResponseMessageBody rspBody = new ResponseMessageBody();
		rspBody.setCode(code);
		rspBody.setMessage(ExceptionFactory.getRspMessage(code));
		return rspBody;
	}

	/**
	 * <pre>
	 * �����Ӧ��͸�����Ϣ�����Ӧ��Ϣ��
	 * </pre>
	 * @param code ��Ӧ��
	 * @param extInfo ������Ϣ
	 * @return ��Ӧ��Ϣ��
	 */
	public static ResponseMessageBody getResponseBodyByCodeAndExtInfo(String code, String extInfo)
	{
		ResponseMessageBody rspBody = new ResponseMessageBody();
		rspBody.setCode(code);
		rspBody.setMessage(ExceptionFactory.getRspMessage(code) + " ������Ϣ{" + extInfo + "}��");
		return rspBody;
	}

	public static ResponseMessageBody getResponseBodyByException(TransException exception)
	{
		ResponseMessageBody rspBody = new ResponseMessageBody();
		rspBody.setCode(exception.getExceptionCode());
		rspBody.setMessage(exception.getExceptionMsg());
		return rspBody;
	}

	public static String getTimestamp(String source, boolean includeTag)
	{
		return StringHelper.getContentByTag(source, Constants.MESSAGE_TIMESTAMP_TAG_BEGIN,
				Constants.MESSAGE_TIMESTAMP_TAG_END, includeTag);
	}

	public static boolean verifyDigest(String source, String msgerPwd, String encoding)
	{
		StringBuffer digestSrc = new StringBuffer();
		digestSrc.append(MessageHelper.getMessageID(source));
		digestSrc.append(MessageHelper.getTimestamp(source, false));
		digestSrc.append(msgerPwd);
		digestSrc.append(MessageHelper.getContentBody(source, true));
		String digest = MD5.encodeString(digestSrc.toString(), encoding);
		String inDigest = MessageHelper.getDigest(source, false);
		return inDigest.equalsIgnoreCase(digest);
	}

	public static boolean verifyRSADigest(String source, String msgerPwd, String encoding)
			throws MessageFormatException
	{
		try
		{
			StringBuffer digestSrc = new StringBuffer();
			digestSrc.append(MessageHelper.getMessageID(source));
			digestSrc.append(MessageHelper.getTimestamp(source, false));
			digestSrc.append(MessageHelper.getContentBody(source, true));
			String inDigest = MessageHelper.getDigest(source, false);
			return SignatureHelper.verifyRSASignature(msgerPwd, inDigest, digestSrc.toString(), encoding);
		}
		catch (Exception e)
		{
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0015);
		}
	}

	public static boolean verifyDigest(TransMessage message, String source, String msgerPwd, String encoding)
			throws MessageFormatException
	{
		boolean flag = false;

		try
		{
			StringBuffer digestSrc = new StringBuffer();
			digestSrc.append(message.getId());
			digestSrc.append(message.getHeader().getTimestamp());
			digestSrc.append(msgerPwd);
			digestSrc.append(StringHelper.getContentByTag(source, Constants.MESSAGE_BODY_TAG_BEGIN,
					Constants.MESSAGE_BODY_TAG_END, true));
			String digest = MD5.encodeString(digestSrc.toString(), encoding);
			String inDigest = StringHelper.getContentByTag(source, Constants.MESSAGE_DIGEST_TAG_BEGIN,
					Constants.MESSAGE_DIGEST_TAG_END, false);
			flag = inDigest.equalsIgnoreCase(digest);
		}
		catch (Exception e)
		{
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0010);
		}

		if (flag)
		{
			return flag;
		}
		else
		{
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0015);
		}
	}

	public static boolean verifyRSADigest(TransMessage message, String source, String msgerPwd, String encoding)
			throws MessageFormatException
	{
		boolean flag = false;

		try
		{
			StringBuffer digestSrc = new StringBuffer();
			digestSrc.append(message.getId());
			digestSrc.append(message.getHeader().getTimestamp());
			digestSrc.append(StringHelper.getContentByTag(source, Constants.MESSAGE_BODY_TAG_BEGIN,
					Constants.MESSAGE_BODY_TAG_END, true));
			String inDigest = StringHelper.getContentByTag(source, Constants.MESSAGE_DIGEST_TAG_BEGIN,
					Constants.MESSAGE_DIGEST_TAG_END, false);
			flag = SignatureHelper.verifyRSASignature(msgerPwd, inDigest, digestSrc.toString(), encoding);
		}
		catch (Exception e)
		{
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0010);
		}

		if (flag)
		{
			return flag;
		}
		else
		{
			logger.info("��ϢժҪ����,��Ϣ��{" + source + "}");
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0015);
		}
	}

	public static boolean verifyTimestamp(String xmlmessage) throws MessageFormatException
	{
		try
		{
			String timestamp = StringHelper.getContentByTag(xmlmessage, Constants.MESSAGE_TIMESTAMP_TAG_BEGIN,
					Constants.MESSAGE_TIMESTAMP_TAG_END, false);
			if (timestamp.length() != Constants.PROTOCOL_TIMESTAMP_FORMAT.length())
			{
				logger.info(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0014));
				throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0014);
			}

			SimpleDateFormat sdf = new SimpleDateFormat(Constants.PROTOCOL_TIMESTAMP_FORMAT);
			Date date = sdf.parse(timestamp);
			Calendar calendar = Calendar.getInstance();

			calendar.add(Calendar.DATE, -1);
			if (date.before(calendar.getTime()))
			{
				throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0014);
			}

			calendar.add(Calendar.DATE, 2);
			if (date.after(calendar.getTime()))
			{
				throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0014);
			}

			return true;
		}
		catch (MessageFormatException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			logger.info(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0014));
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0014);
		}
	}

	public static boolean verifyTimestamp(TransMessage message) throws MessageFormatException
	{
		try
		{
			String timestamp = message.getHeader().getTimestamp();
			if (timestamp.length() != Constants.PROTOCOL_TIMESTAMP_FORMAT.length())
			{
				logger.info(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0014));
				throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0014);
			}

			SimpleDateFormat sdf = new SimpleDateFormat(Constants.PROTOCOL_TIMESTAMP_FORMAT);
			Date date = sdf.parse(timestamp);
			Calendar calendar = Calendar.getInstance();

			calendar.add(Calendar.DATE, -1);
			if (date.before(calendar.getTime()))
			{
				throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0014);
			}

			calendar.add(Calendar.DATE, 2);
			if (date.after(calendar.getTime()))
			{
				throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0014);
			}

			return true;
		}
		catch (MessageFormatException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			logger.info(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0014));
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0014);
		}
	}

	public static boolean verifyVersion(TransMessage message, String version) throws MessageFormatException
	{
		if (message.getVersion().equalsIgnoreCase(version))
		{
			return true;
		}
		else
		{
			logger.info(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0011));
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0011);

		}
	}

	public static String getVersion(String message) throws MessageFormatException
	{
		String id = null;
		String flag = Constants.MESSAGE_VERSION_TAG;
		int index = message.lastIndexOf(flag);
		if (-1 != index)
		{
			String tmp = message.substring(index + flag.length());
			index = tmp.indexOf("\"");
			id = tmp.substring(0, index);
		}
		return id;
	}

	public static boolean verifyTransType(TransMessage message, int transType) throws MessageFormatException
	{
		if (message.getHeader().getTransactionType() == transType)
		{
			return true;
		}
		else
		{
			logger.info(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0010));
			throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0010);

		}
	}

	public static boolean verifyMessageId(TransMessage message) throws MessageFormatException
	{
		MessageBody body = (MessageBody) message.getBody();
		if ((body != null) && (body instanceof RequestMessageBody))
		{
			try
			{
				String msgid = message.getId();

				String msgidpart = message.getHeader().getMessengerID()
						+ message.getHeader().getTimestamp().substring(0,
								Constants.MESSAGE_MESSAGEID_TIMESTAMP_FORMAT.length());

				if (msgid.startsWith(msgidpart))
				{
					String sequence = msgid.substring(msgidpart.length());
					if (!sequence.matches("[0-9]+"))
					{
						logger.info(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0012)
								+ ", ��ˮ��{" + sequence + "}, ��ϢID{" + msgid + "}");
						throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0012);
					}
				}
				else
				{
					logger.info(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0012)
							+ ", ������ID+����{" + msgidpart + "}, ��ϢID{" + msgid + "}");
					throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0012);
				}
			}
			catch (MessageFormatException e)
			{
				throw e;
			}
			catch (Exception e)
			{
				logger.error(ExceptionFactory.getRspMessage(Constants.TRANSACTION_RESPONSE_CODE_0012) + ", �쳣��Ϣ:"
						+ e.getMessage());
				throw ExceptionFactory.getMsgException(Constants.TRANSACTION_RESPONSE_CODE_0012);
			}
		}
		return true;
	}

	public static String getParameter(String source, String parameter)
	{
		String paramValue = null;
		String[] keyvalus = source.split("&");
		for (int i = 0; i < keyvalus.length; i++)
		{
			String string = keyvalus[i];
			int index = string.indexOf("=");
			if (index > 0)
			{
				String key = string.substring(0, index);
				if (key.equalsIgnoreCase(parameter))
				{
					paramValue = string.substring(index + 1, string.length());
					break;
				}
			}
		}
		return paramValue;
	}
}
